// components/home-select/index.js
const App = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    defaultOptionValue: String,
    isDict: {
      type: Boolean,
      value: true,
    },
    isNowSearch:{
      type: Boolean,
      value: true,
    },
    // value: {
    //   type: String,
    //   value: ''
    // },
    code: {
      type: String,
      value: "fwlb",
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    defaultIndex: 0,
    hoverClick: "",
    label: "",
    sumbitInfo: {},
    value: "",
    show: false,
    columns: [],
  },
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () {
      // console.log('this',this);
      // console.log('App.globalData.dictTree',App.globalData.dictTree);
      if (this.properties.isDict) {
        let dict = App.globalData.dictTree;
        for (let i = 0; i < dict.length; i++) {
          if (dict[i].sKey == this.properties.code) {
            this.setData({
              columns: dict[i].Child,
            });
          }
        }
      } else {
        let bannerOptions = App.globalData.bannerOptions;
        for (let i = 0; i < bannerOptions.length; i++) {
          bannerOptions[i].sKey = bannerOptions[i].sId;
          this.setData({
            columns: bannerOptions,
          });
        }
      }

      // 有值回显默认值
      if (this.properties.defaultOptionValue) {
        if (this.data.columns && this.data.columns.length > 0) {
          let i = this.data.columns.findIndex((item) => {
            return item.sKey == this.properties.defaultOptionValue;
          });
          this.setData({
            label: this.data.columns[i].sName,
          });
          if(this.properties.isNowSearch) {
            this.triggerEvent("toSumbit", {
              name: this.data.columns[i].sName,
              code: this.data.columns[i].sKey,
              path: this.data.columns[i].sPageUrl,
              defaultIndex: Number(i),
            });
          }
          
        }
      } else {
        // 无值默认选中第一个
        if (this.data.columns && this.data.columns.length > 0) {
          this.setData({
            label: this.data.columns[0].sName,
          });
          this.triggerEvent("toSumbit", {
            name: this.data.columns[0].sName,
            code: this.data.columns[0].sKey,
            path: this.data.columns[0].sPageUrl,
            defaultIndex: 0,
          });
        }
      }
    },
    hide: function () {},
    resize: function () {},
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onClick() {
      this.setData({
        hoverClick: "#faab41",
        show: true,
      });
      setTimeout(() => {
        this.setData({
          hoverClick: "",
        });
      }, 400);
    },
    onClose: function () {
      this.setData({
        show: false,
      });
    },
    onChange: function () {},
    onConfirm: function (e) {
      this.setData({
        show: false,
        value: e.detail.value.sName,
      });
      this.triggerEvent("toSumbit", {
        name: e.detail.value.sName,
        code: e.detail.value.sKey,
        path: e.detail.value.sPageUrl,
      });
    },
  },
});
